package com.loop.unclelend.ui.presenter

import com.loop.unclelend.app_config.UncleConfigs
import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.ApiService.addCommonParams
import com.loop.unclelend.network.ApiService.addRandomParams
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Repay
import org.json.JSONArray
import org.json.JSONObject

/**
 * UnncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/27 13:45
 * @Annotation：待还款订单列表
 */
class Pre_Repay(private val iView: IView_Repay): ProPresenter(iView) {

    /**
     * 获取订单列表
     */
    fun requestRepayOrderListAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_LOAN_ORDER_PATH)
        }
        doPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    val loanList = obj.optJSONArray("loanList")
                    val orderList = ArrayList<JSONObject>()
                    if (loanList != null && loanList.length() > 0) {
                        for (i in 0..<loanList.length()){
                            val item = loanList.optJSONObject(i)
                            //非待还款不显示
                            if ("WAIT_REPAY" != item.optString("orderStatus")){
                                continue
                            }
                            val titleList = JSONArray()
                            val contentList = JSONArray()
                            val array = item.optJSONArray("list")
                            if (array != null) {
                                for (index in 0..<array.length()){
                                    val model = array.optJSONObject(index)
                                    if (model.has("title") && "YES" == model.optString("title")){
                                        titleList.put(model)
                                    } else {
                                        contentList.put(model)
                                    }
                                }
                            }
                            item.put("titleList",titleList)
                            item.put("contentList",contentList)
                            orderList.add(item)
                        }
                        iView.showOrderList(orderList)
                    }
                    if (orderList.size == 0){
                        iView.showEmptyView()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }

    /**
     * 订单还款
     * 不减免 normal
     * 积分减免 score
     * 优惠券减免 coupon
     */
    fun requestPayLinkAction(orderId: String){
        iView.showUncleLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("orderId",orderId)
            put("path",ApiService.API_PATH.UNCLE_API_LOAN_REPAYMENT_URL_PATH)
        }
        if (UncleConfigs.isUgx() || UncleConfigs.isKsh()){//肯尼亚、乌干达 需要使用 getOrderRepayH5 获取链接
            params.put("path",ApiService.API_PATH.UNCLE_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH)
        }
        doPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideUncleLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    var linkUrl = ""
                    val url = obj.optString("url")
                    if (url.isNotEmpty()){
                        linkUrl = url
                    }

                    val repayUrl = obj.optString("repayUrl")
                    if (repayUrl.isNotEmpty()){
                        linkUrl = repayUrl
                    }

                    //没有还款链接 - 需要进入还款状态页查询还款状态
                    if (linkUrl.isEmpty()){
                        val thirdOrderId = obj.optString("thirdOrderId")
                        iView.showPaymentStatusPage(orderId,thirdOrderId)
                    } else {
                        iView.showLoanPayLink(linkUrl)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }

}